//
//  LoginViewController.swift
//  YueJian
//
//  Created by mac on 2017/10/21.
//  Copyright © 2017年 MapleStory. All rights reserved.
//

import UIKit
import Alamofire


class LoginViewController: UIViewController {

    var password :UITextField?
    var account:UITextField?
    let Swidth = UIScreen.main.bounds.width
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view = UIImageView(image: UIImage(named: "c"))
        view.isUserInteractionEnabled = true
        self.view.backgroundColor = UIColor.white
        account = UITextField(frame: CGRect(x: 30, y: 150, width: Swidth-60, height: 50))
        account?.placeholder = "账号"
        account?.borderStyle = .line
        self.view.addSubview(account!)
     
        password = UITextField(frame: CGRect(x: 30, y: 220, width: Swidth-60, height: 50))
        password?.placeholder = "密码"
        password?.borderStyle = .line
        password?.isSecureTextEntry = true
        self.view.addSubview(password!)
        let button = UIButton(frame: CGRect(x: 30, y: 360, width: Swidth-60, height: 50))
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitle("登录", for: UIControlState.normal)
        button.addTarget(self, action: #selector(login), for: UIControlEvents.touchUpInside)
        button.backgroundColor = UIColor.black
        self.view.addSubview(button)
        
        let registerBtn = UIButton(frame: CGRect(x: 30, y: 420, width: Swidth-60, height: 50))
        registerBtn.setTitleColor(UIColor.white, for: .normal)
        registerBtn.setTitle("注册", for: UIControlState.normal)
        registerBtn.addTarget(self, action: #selector(pushRegisterVc), for: UIControlEvents.touchUpInside)
        registerBtn.backgroundColor = UIColor.black
        self.view.addSubview(registerBtn)
     
    }
    
    @objc func login() {

        Alamofire.request(URL(string: "http://172.24.10.175/workout/api.php/login")!, method: .get, parameters: ["tel":account?.text ?? 0,"password":password?.text ?? 3], encoding: URLEncoding.default, headers: nil).responseJSON { (responcse) in

            print(responcse)
            let dic = responcse.result.value as! NSDictionary
            let keys = dic.allKeys as! NSArray
            if keys.contains("error") {
                let alert = UIAlertController(title: "提示", message: "账号或密码错误", preferredStyle: .alert)
                let cancel = UIAlertAction(title: "确定", style: .cancel, handler: nil)
                alert.addAction(cancel)//比较明显额我都给你加上了,其他的好像没有提示了//键盘回收你会吗、、你的账号密码，因为不遮盖的原因，我没给你回收
                self.present(alert, animated: true, completion: nil)
            } else {
                let sessionId = dic["sessionid"] as! String
//                let id = dic["sessionid"]
                UserDefaults.standard.set(sessionId, forKey: "sessionId")
                let ap = UIApplication.shared.delegate as! AppDelegate
                ap.window?.rootViewController = TabBarController()
                
            }
        }
    }
    
    @objc func pushRegisterVc() {
        let vc = RegisterViewController();
        self.present(vc, animated: true) {
            
        };
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
